#include <iostream>
#include <random>
#define swap(i, j){int tmp=i; i=j; j=tmp;}
using namespace std;
std::random_device rd;
std::mt19937 gen(rd());
int Partition(int* arr, int p, int r){
int x=arr[r];
int i=p-1;
for(int j=p; j<r; ++j){
if(arr[j]<=x){
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i+1], arr[r]);
return i+1;
}
int Randomized_Partition(int* arr, int p, int r){
std::uniform_int_distribution<int> runif(p, r);
int i=runif(gen);
swap(arr[r], arr[i]);
return Partition(arr, p, r);
}
void Randomized_QuickSort(int* arr, int p, int r){
if(p<r){
int q=Randomized_Partition(arr, p, r);
Randomized_QuickSort(arr, p, q-1);
Randomized_QuickSort(arr, q+1, r);
}
}
int main(void){
int arr[]={5, 3, 6, 2, 7, 9};
int size=sizeof(arr)/sizeof(int);
Randomized_QuickSort(arr, 0, size-1);
for(int i=0; i<size; ++i) cout<<arr[i]<<" ";
cout<<endl;
return 0;
}